﻿var dRowObject = new Array();
var dRowObjectId = new Array();

var dRowMaxId = 0;

function InsertRow(controlId) {
    AddRow(controlId);
}

function DeleteRow(obj, controlId) {
	
	var dRowContainer = $("#" + controlId);
	
	var bindRowCount=1;
	if(dRowContainer.attr("bindRowCount")!=undefined)
	{
		bindRowCount = dRowContainer.attr("bindRowCount");
	}
	
	Confirm('确定要执行删除操作？', function() {
		
		//确定
		var parentObj = $(obj).parents(".processData");
		var deleteIndex = parentObj.index();
		var rows = $("#" + controlId + " .processData");
		for(var i=0;i<rows.length;i++)
		{
			if(i==deleteIndex)
			{
				for(var j=0;j<bindRowCount;j++)
				{
					$(rows[i+j]).remove();
				}
				
				break;
			}
		}
		
		//dRowCount--;

	});
}

function ShowRow()
{
	var formObj = $("form");
	if (formObj.length == 0) {
		Alert("页面缺少必要的form标签");
		return false;
	}
	
	var dyn = $(".DynamicRow");
	var count = dyn.length;
	for (var i = 0; i < count; i++) {
		
		var controlId = dyn[i].id;
		
		var dRowContainer = $("#" + controlId);
		
		//数组序列
		dRowObjectId[i]=controlId;

		dRowObject[i]=$("#" + controlId).children().clone();
		
		if(dRowObject[i][0].tagName.toLowerCase()=="tbody")
		{
			//表格处理
			dRowObject[i] = dRowObject[i].children(0);
		}
		
		var dataParam = dRowContainer.attr("data");
		var dataObject = eval(decodeURIComponent(eval(dataParam)));
		/*
		if(dRowContainer.attr("bindRowCount")!=undefined)
		{
			bindRowCount = dRowContainer.attr("bindRowCount");
		}
		*/
		
		//清空所有模板行
		$("#" + controlId).children().remove();
		
		if(dataObject!=undefined)
		{
			for(var j=0 ; j < dataObject.length;j++)
			{	
				AddRow(controlId,dataObject[j]);
			}
		}
		
		//动态行Json
		var input = document.createElement("input");
		input.type = "hidden";
		input.id = "hid" + controlId;
		input.name = "hid" + controlId;
		input.value = "";
		formObj.append($(input));
    }	
		
}

function PrintXml()
{
	var dyn = $(".DynamicRow");
	var count = dyn.length;
	for (var i = 0; i < count; i++) {
		
		var controlId = dyn[i].id;
		
		var bindRowCount=1;
		if($("#"+controlId).attr("bindRowCount")!=undefined)
		{
			bindRowCount = $("#"+controlId).attr("bindRowCount");
		}
		

		var dRowCount = GetRow(controlId,bindRowCount);
		var objArray = new Array(dRowCount);
		for(var k=0;k<dRowCount;k++){
			objArray[k] = {};
		}

		var b = $("#"+controlId).find("[id]");
		for(var j=0;j<b.length;j++)
		{
			var currentId = b[j].id;
			var idArray = currentId.split("_");
			if(idArray.length==3)
			{
				var key=idArray[1];
				
				//index是当前行所在容器的位置
				var index = $($(b[j]).parents(".processData")[0]).index();
				index = Math.floor(index/bindRowCount);
				
				//判断是radio或checkbox
				if(b[j].nodeName.toLowerCase()=="input" && (b[j].type.toLowerCase()=="radio" || b[j].type.toLowerCase()=="checkbox"))
				{
					if(b[j].checked)
					{
						eval("objArray[index]." + key + "=$(b[j]).val();");
					}
				}
				else
				{
					eval("objArray[index]." + key + "=$(b[j]).val();");
				}
				
			}
		}
		
		var s = JSON.stringify(objArray);
		s = s.replace(/\%/g, "%25");
		s = s.replace(/\+/g, "%2B");
		s = s.replace(/\&/g, "%26");
		s = encodeURIComponent(s);
		$("#hid"+controlId).val(s);
		
	}
}

function GetRow(controlId,bindRowCount)
{
	var ret=0;
	
	var obj=$("#"+controlId);
	if(obj.is("table"))
	{
		ret=$("#"+controlId+" tr").length;
	}
	else
	{
		ret=obj.children().length;
	}
	
	ret=ret/bindRowCount;
	
	return ret;
}

function AddRow(controlId,dataItem) {
	
	var dRowContainer = $("#" + controlId);
	
	var currentObj;
	for(var i=0;i<dRowObject.length;i++)
	{
		if(dRowObjectId[i]==controlId)
		{
			currentObj = dRowObject[i].clone();
		}
	}
	
	
	//处理id
	currentObj.find("[id]").each(function(index,item){
		var elementId = item.id;
		
		if(dataItem!=undefined)
		{
			var itemValue = eval("dataItem." + elementId);
			
			if(item.nodeName.toLowerCase()=="input" && (item.type.toLowerCase()=="radio" || item.type.toLowerCase()=="checkbox"))
			{
				if(itemValue!=undefined)
				{
					item.checked=true;
				}
			}
			else
			{
				$(item).val(itemValue);	
			}
			
			
		}
		
		item.id = controlId + "_" + elementId + "_" + dRowMaxId;
	})
	
	//处理name
	currentObj.find("[name]").each(function(index,item){
		var elementId = item.name;
		
		item.name = controlId + "_" + elementId + "_" + dRowMaxId;
	})
	

	
	currentObj.addClass("processData");
	currentObj.appendTo(dRowContainer);
	
	//dRowCount++;
	dRowMaxId++;
	
}
